package com.foreknow.dao.impl;

import com.foreknow.dao.IBaseDao;
import com.foreknow.entity.User;
import com.foreknow.util.DbHelper;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.ResultSetHandler;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class UserDaoImpl implements IBaseDao<User> {
    private static QueryRunner queryRunner = DbHelper.getQueryRunner();

    @Override
    public int delete(String sql, Object... params) throws SQLException {
        return queryRunner.update(sql,params);
    }

    @Override
    public int delete(String sql) throws SQLException {
        return queryRunner.update(sql);
    }

    @Override
    public int insert(String sql, Object[] params) throws SQLException {
        return queryRunner.update(sql,params);
    }

    @Override
    public List<User> queryByCondition(String sql) throws SQLException {
        return null;
    }

    public ResultSetHandler<List<User>> getListHandler(){

        ResultSetHandler<List<User>> handler = new ResultSetHandler<List<User>>(){

            @Override
            public List<User> handle(ResultSet resultSet) throws SQLException {
                List<User> users = new ArrayList<>();
                while (resultSet.next()){
                    Integer id = resultSet.getInt("id");
                    String username = resultSet.getString("username");
                    String account = resultSet.getString("account");
                    String password = resultSet.getString("password");
                    String phone = resultSet.getString("phone");
                    String email = resultSet.getString("email");
                    String avatar = resultSet.getString("avatar");

                    User user = new User(id,username,account,password,phone,email,avatar);
                    users.add(user);
                }
                return users;
            }
        };
        return handler;
    }

    @Override
    public List<User> queryByCondition(String sql, Object... params) throws SQLException {
        return queryRunner.query(sql,getListHandler(),params);
    }

    @Override
    public User queryOne(String sql, Object param) throws SQLException {
        return queryRunner.query(sql,getResultSetHandler(),param);
    }

    @Override
    public User queryOne(String sql, Object[] params) throws SQLException {
        return queryRunner.query(sql,getResultSetHandler(),params);
    }

    @Override
    public int update(String sql, Object... params) throws SQLException {
        return queryRunner.update(sql,params);
    }

    @Override
    public int update(String sql, Object param) throws SQLException {
        return queryRunner.update(sql,param);
    }

    @Override
    public ResultSetHandler<User> getResultSetHandler() {
        ResultSetHandler<User> handler = new ResultSetHandler<User>(){

            @Override
            public User handle(ResultSet resultSet) throws SQLException {
                User user = null;
                while (resultSet.next()){
                    Integer id = resultSet.getInt("id");
                    String username = resultSet.getString("username");
                    String account = resultSet.getString("account");
                    String password = resultSet.getString("password");
                    String phone = resultSet.getString("phone");
                    String email = resultSet.getString("email");
                    String avatar = resultSet.getString("avatar");

                    user = new User(id,username,account,password,phone,email,avatar);
                }
                return user;
            }
        };
        return handler;
    }
}